package com.campus.controller;

import com.campus.model.Course;
import com.campus.model.LearnUnit;
import com.campus.service.CourseService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
@RestController
@RequestMapping("/courses")
public class CourseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @PostMapping("/create")
    public ResponseEntity<String> createCourse(@RequestBody Course course) {
        int result = courseService.createCourse(course);
        return result > 0 ? ResponseEntity.ok("Course created successfully") : ResponseEntity.status(500).body("Failed to create course");
    }

    @GetMapping("/{courseId}")
    public ResponseEntity<Course> getCourseById(@PathVariable int courseId) {
        Course course = courseService.getCourseById(courseId);
        return course != null ? ResponseEntity.ok(course) : ResponseEntity.status(404).body(null);
    }

    @GetMapping("/all")
    public ResponseEntity<List<Course>> getAllCourses() {
        List<Course> courses = courseService.getAllCourses();
        return ResponseEntity.ok(courses);
    }

    @PostMapping("/{courseId}/learn-units/create")
    public ResponseEntity<String> createLearnUnit(@PathVariable int courseId, @RequestBody LearnUnit learnUnit) {
        learnUnit.setCourseId(courseId);
        int result = courseService.createLearnUnit(learnUnit);
        return result > 0 ? ResponseEntity.ok("Learn unit created successfully") : ResponseEntity.status(500).body("Failed to create learn unit");
    }
    @GetMapping("/learn-units/{unitId}")
    public ResponseEntity<LearnUnit> getLearnUnitById(@PathVariable int unitId) {
        LearnUnit learnUnit = courseService.getLearnUnitById(unitId);
        return learnUnit != null ? ResponseEntity.ok(learnUnit) : ResponseEntity.status(404).body(null);
    }

    @GetMapping("/{courseId}/learn-units")
    public ResponseEntity<List<LearnUnit>> getLearnUnitsByCourseId(@PathVariable int courseId) {
        List<LearnUnit> learnUnits = courseService.getLearnUnitsByCourseId(courseId);
        return ResponseEntity.ok(learnUnits);
    }
}
